<!DOCTYPE html>
<html lang="zh-CN">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript">
    $.urlParam = function (name) {
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results == null) {
            return null;
        }
        else {
            return decodeURI(results[1]) || 0;
        }
    }

    function randomString(e) {
        e = e || 32;
        var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
            n = t.length,
            r = "";
        for (i = 0; i < e; i++) r += t.charAt(Math.floor(Math.random() * n));
        return r;
    }

    var userData = {
        "accountId": "5840",
        "authCardCount": "10",
        "phone": "",
        "isAuthPhone": "0",
        "nickname": "5LiR55S35p2l5LqG",
        "avatar": "http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJhibVwX1zg8ibgPxfzPy03eEbwlCiamRDyrD34ic90Igm0YEpTReiaeS2dM4FicopKIxXjuFBufiaRB4e5A/0",
    };

    var globalData = {
        "card": "899",
        "isNotyMsg": '0',
        "notyMsg": '',
        "notyTime": '0',
        "notySpeed": 4000,
        "isAlertMsg": '0',
        "alertMsg": '',
        "fileUrl": "http://test.anoqo.cn/",
        "imageUrl": "http://test.anoqo.cn/",
        "session": 'ZWE0NWM3MTQwMjM5NjBlY2JhMmJkMzIyZjZmZmI4Y2M=',
//        "baseUrl":"http://test.anoqo.cn/",
        "baseUrl": "http://127.0.0.1/game/",
        "openId": "",
        "dealerNum": "13",
        "sangongAlert": false,
        "sangongAlertText": "游戏部署中，敬请期待",
    };

    var url = $.urlParam('local') == 1 ? "ws://127.0.0.1:16669" : "ws://47.104.14.221:16669";
    var socketStatus = 0;

    var createInfo = {
        isShow: 0,
        flower: {
            chip_type: 1,
            ticket_count: 1,
            pkvalue1: 0,
            pkvalue2: 0,
            upper_limit: 1e3
        },
        landlord: {
            ticket_count: 1,
            base_score: 10,
            ask_mode: 1
        },
        majiang: {
            joker: 0,
            horse_count: 0,
            qianggang: 0,
            ticket_count: 1,
            chengbao: 0
        },
        bull: {
            ticket_type: 3,
            score_type: 3,
            rule_type: 1,
            is_cardfive: 0,
            is_cardbomb: 0,
            is_cardtiny: 0,
            banker_mode: 2,
            banker_score: 4,
            banker1: "unselected",
            banker2: "selected",
            banker3: "unselected",
            banker4: "unselected",
            banker5: "unselected"
        },
        bull9: {
            ticket_type: 1,
            score_type: 3,
            rule_type: 1,
            is_cardfive: 0,
            is_cardbomb: 0,
            is_cardtiny: 0,
            banker_mode: 2,
            banker_score: 4,
            banker1: "unselected",
            banker2: "selected",
            banker3: "unselected",
            banker4: "unselected",
            banker5: "unselected"
        },
        bullfight: {
            ticket_type: 1,
            score_type: 3,
            rule_type: 1,
            is_cardfive: 0,
            is_cardbomb: 0,
            is_cardtiny: 0,
            is_cardfour: 0,
            banker_mode: 1,
            banker_score: 1
        },
        sangong: {
            ticket_type: 1,
            score_type: 3,
            //rule_type: 1,
            banker_mode: 1,
            //banker_score: 1,
            //is_cardjoker: 0,
            //is_cardbao9: 0,
			is_cardtiny: 0
        }
    };

    function CreateRoom() {
        ws = new WebSocket(url);
        ws.onopen = function () {
            // 每2秒发@, 连续3次收不到则刷新页面
            setInterval(function () {
                socketStatus += 1;
                ws.send("@");
                (socketStatus > 3 || socketStatus > 3) && window.location.reload()
            }, 2000);

			ws.send(JSON.stringify({
                operation: "CreateRoom",
                account_id: userData.accountId,
                session: globalData.session,
                data: {
                    data_key: Date.parse(new Date) + randomString(5),
                    ticket_type: createInfo.sangong.ticket_type,
                    score_type: createInfo.sangong.score_type,
                    //rule_type: createInfo.sangong.rule_type,
                    banker_mode: createInfo.sangong.banker_mode,
                    //banker_score_type: createInfo.sangong.banker_score,
                    is_cardtiny: createInfo.sangong.is_cardtiny
                }
            }));
        };

        ws.onmessage = function (e) {
            if ("@" == e.data) {
                console.log("@");
                return socketStatus = 0;
            }
            //alert("收到服务端的消息：" + e.data);
            console.log(e.data);
            var obj = eval("(" + e.data + ")");
            alert(obj.result_message + "room_id=" + obj.data.room_id + "room_number=" + obj.data.room_number);
        };

        ws.onerror = function (e) {
            alert("WebSocketError");
            console.log("WebSocketError!");
        };

        ws.onclose = function (e) {
            alert("WebSocket closed!");
            console.log("WebSocket closed!");
        };

    }

    function PrepareJoinRoom() {
        ws = new WebSocket(url);
        ws.onopen = function () {
            ws.send(JSON.stringify({
                operation: "PrepareJoinRoom",
                account_id: $("#account_id").val(),
                data: {
                    room_number: $("#room_number").val()
                }
            }));
        };

        ws.onmessage = function (e) {
            if ("@" == e.data) {
                console.log("@");
                return socketStatus = 0;
            }
            //alert("收到服务端的消息：" + e.data);
            console.log(e.data);
            var obj = eval("(" + e.data + ")");
            alert(obj.result_message);
        };

        ws.onerror = function (e) {
            alert("WebSocketError");
            console.log("WebSocketError!");
        };

        ws.onclose = function (e) {
            alert("WebSocket closed!");
            console.log("WebSocket closed!");
        };

    }

    function JoinRoom() {
        ws = new WebSocket(url);
        ws.onopen = function () {
            ws.send(JSON.stringify({
                operation: "JoinRoom",
                account_id: $("#account_id").val(),
                data: {
                    room_number: $("#room_number").val()
                }
            }));
        };

        ws.onmessage = function (e) {
            if ("@" == e.data) {
                console.log("@");
                return socketStatus = 0;
            }
            //alert("收到服务端的消息：" + e.data);
            console.log(e.data);
            var obj = eval("(" + e.data + ")");
            alert(obj.result_message);
        };

        ws.onerror = function (e) {
            alert("WebSocketError");
            console.log("WebSocketError!");
        };

        ws.onclose = function (e) {
            alert("WebSocket closed!");
            console.log("WebSocket closed!");
        };

    }

    function ReadyStart() {
        ws = new WebSocket(url);
        ws.onopen = function () {
            ws.send(JSON.stringify({
                operation: "ReadyStart",
                account_id: $("#account_id").val(),
                data: {
                    room_id: $("#room_id").val()
                }
            }));
        };

        ws.onmessage = function (e) {
            if ("@" == e.data) {
                console.log("@");
                return socketStatus = 0;
            }
            //alert("收到服务端的消息：" + e.data);
            console.log(e.data);
            var obj = eval("(" + e.data + ")");
            alert(obj.result_message);
        };

        ws.onerror = function (e) {
            alert("WebSocketError");
            console.log("WebSocketError!");
        };

        ws.onclose = function (e) {
            alert("WebSocket closed!");
            console.log("WebSocket closed!");
        };

    }

    function KickCard() {
        ws = new WebSocket(url);
        ws.onopen = function () {
            ws.send(JSON.stringify({
                operation: "KickCard",
                account_id: $("#account_id").val(),
                data: {
                    card_id: $("#card_id").val()
                }
            }));
        };

        ws.onmessage = function (e) {
            if ("@" == e.data) {
                console.log("@");
                return socketStatus = 0;
            }
            //alert("收到服务端的消息：" + e.data);
            console.log(e.data);
            var obj = eval("(" + e.data + ")");
            alert(obj.result_message);
        };

        ws.onerror = function (e) {
            alert("WebSocketError");
            console.log("WebSocketError!");
        };

        ws.onclose = function (e) {
            alert("WebSocket closed!");
            console.log("WebSocket closed!");
        };

    }

</script>
<div class="container">
    <form>
        <div class="form-group">
            <button type="button" class="btn btn-success" onclick="CreateRoom();">CreateRoom</button>
        </div>
        <hr/>

        <div class="form-group">
            <label for="account_id">Account ID</label>
            <input type="text" class="form-control" id="account_id" placeholder="Account ID" value="5841">
        </div>
        <div class="form-group">
            <label for="room_number">Room Number</label>
            <input type="text" class="form-control" id="room_number" placeholder="Room Number">
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-success" onclick="PrepareJoinRoom();">PrepareJoinRoom</button>
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-success" onclick="JoinRoom();">JoinRoom</button>
        </div>
        <div class="form-group">
            <label for="room_id">Room ID</label>
            <input type="text" class="form-control" id="room_id" placeholder="Room ID">
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-success" onclick="ReadyStart();">ReadyStart</button>
        </div>

        <div class="form-group">
            <label for="card_id">Card</label>
            <input type="text" class="form-control" id="card_id" placeholder="Card ID">
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-success" onclick="KickCard();">KickCard</button>
        </div>


    </form>

</div>
</html>


